home *** CD-ROM | disk | FTP | other *** search
- /* Copyright 1992 H.Ogasawara(COR.) */
-
- #include "proto.h"
- #include "run.h"
-
- /*
- 3 modes only!
-
- X-MODEM SUM
- X/Y-MODEM(CRC)
- Y-MODEM-G
- */
- static void ReceiveRun3(),
- ReceiveRun2(),
- ReceiveRun1();
- void ReceiveStart();
-
- enum { retSUM, retCRC, retG };
-
- #define GWAIT (16*100)
-
- unsigned short rettype= retCRC;
- unsigned int firstcode, /* size */
- firstsize;
-
- static char xmodemfile[80]; /* 1992 9/15 */
-
- Write232C( code )
- unsigned char code;
- {
- unsigned int time= ONTIME()+ 6000;
- while( !OSNS232C() ){
- if( (B_KEYSNS()&0xff) == 0x03 ){
- if( (B_KEYINP()&0xff) == 0x03 ){
- allbreak();
- return -1;
- }
- }else if( ONTIME() > time ){
- allbreak();
- return -1;
- }
- }
- OUT232C( code );
- return 0;
- }
-
- ReceiveEnd()
- {
- if( batch )
- ReceiveStart();
- else
- EndProgram();
- }
-
- /* Get Packet DATA & check Y */
- static void
- ReceiveRun4( ptr )
- unsigned char *ptr;
- {
- if( DataUnpack( firstsize, ptr, rettype == retSUM ) < 0 ){
- /* Error */
- if( rettype != retG ){
- if( Write232C( NAK ) < 0 )
- return;
- }else{
- Write232C( CAN );
- allbreak();
- Write232C( CAN );
- return;
- }
- run= ReceiveRun3;
- timeout= GWAIT+ONTIME();
- runsize= 1;
- }else{
- /* OK */
- if( rettype != retG )
- if( Write232C( ACK ) < 0 )
- return;
- if( *ptr == 0 && firstsize == 128 ){ /* Y-MODEM */
- if( batch ){
- unsigned short i;
- for( i= 0 ; i< 128 ; i++ ){
- if( (ptr+SKIP)[i] )
- goto notend;
- }
- batch= 0;
- timeout= runsize= 0;
- ReceiveEnd();
- return;
- }
- notend:
- filetime= GetFilePacket( ptr+SKIP, filename, &filesize );
- last= (filesize>>7)+1;
- if( !OpenFileW( filename ) ){
- Write232C( CAN );
- Write232C( CAN );
- return;
- }
- Write232C( firstcode );
- run= ReceiveRun1;
- }else{
- /* strcpy( filename, DEFAULTNAME ); 1992 9/15 */
- strcpy( filename, *xmodemfile ? xmodemfile : DEFAULTNAME );
- if( !OpenFileW( filename ) ){
- Write232C( CAN );
- Write232C( CAN );
- return;
- }
- filetime= filesize= 0;
- if( WriteFile( ptr+SKIP, firstsize ) < 0 )
- return;
- block+= firstsize>>7;
- size+= firstsize;
- run= ReceiveRun3;
- }
- DispFilename();
- DispCountup();
- timeout= GWAIT+ONTIME();
- runsize= 1;
- }
- }
-
- /* Get DATA HEAD */
- static void
- ReceiveRun3( ptr )
- unsigned char *ptr;
- {
- if( !ptr ){
- Write232C( CAN );
- allbreak();
- Write232C( CAN );
- }else{
- if( *ptr == SOH || *ptr == STX ){
- firstsize= *ptr == SOH ? 128 : 1024;
- run= ReceiveRun2;
- runsize= firstsize+SKIP+(rettype == retSUM ? 1 : 2);
- timeout= 0;
- }else if( *ptr == EOT ){
- Write232C( ACK );
- CloseFile();
- timeout= runsize= 0;
- ReceiveEnd();
- }else{
- if( *ptr != CAN )
- Write232C( CAN );
- allbreak();
- }
- }
- }
-
- /* Get Packet DATA */
- static void
- ReceiveRun2( ptr )
- unsigned char *ptr;
- {
- if( DataUnpack( firstsize, ptr, rettype == retSUM ) < 0 ){
- if( rettype != retG ){
- if( Write232C( NAK ) < 0 )
- return;
- }else{
- Write232C( CAN );
- allbreak();
- Write232C( CAN );
- return;
- }
- run= ReceiveRun3;
- timeout= GWAIT+ONTIME();
- runsize= 1;
- }else{
- if( rettype != retG )
- if( Write232C( ACK ) < 0 )
- return;
- if( WriteFile( ptr+SKIP, firstsize ) < 0 )
- return;
- run= ReceiveRun3;
- timeout= GWAIT+ONTIME();
- runsize= 1;
- block+= firstsize>>7;
- size+= firstsize;
- DispCountup();
- }
- }
-
- /* Check TOP Packet */
- static void
- ReceiveRun1( ptr )
- unsigned char *ptr;
- {
- if( !ptr ){
- Write232C( firstcode );
- }else{
- if( *ptr == SOH || *ptr == STX ){
- firstsize= *ptr == SOH ? 128 : 1024;
- run= *filename ? ReceiveRun2 : ReceiveRun4;
- runsize= firstsize+SKIP+(rettype == retSUM ? 1 : 2);
- timeout= 0;
- }else{
- if( *ptr != CAN && *ptr != EOT )
- Write232C( CAN );
- allbreak();
- }
- }
- }
-
- void
- ReceiveStart()
- {
- static unsigned char first[3]= { NAK, __C, __G };
- firstcode= first[ rettype ];
- if( Write232C( firstcode ) < 0 )
- return;
- run= ReceiveRun1;
- timeout= 930+ONTIME();
- strcpy( xmodemfile, filename );
- *filename= '\0';
- block= size= last= 0;
- runsize= 1;
- }
-
-